package com.crm.crm.controller;

import com.crm.crm.domain.Contract;
import com.crm.crm.domain.dto.contract.ContractDto;
import com.crm.crm.domain.dto.contract.ContractPageQueryParams;
import com.crm.crm.domain.dto.contract.ContractSaveDTO;
import com.crm.crm.domain.vo.contract.ContractVo;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;
    //修改合同
    @PutMapping
    public Result getContractById(@RequestBody ContractSaveDTO contractSaveDTO) {
        contractService.updateContractById(contractSaveDTO);
        return Result.success();
    }
    //合同详情
    @GetMapping("/{id}")
    public Result<ContractVo> getContractDetailsById(@PathVariable("id") Long id) {
        ContractVo contract = contractService.getContractDetailsById(id);
        return Result.success(contract);
    }
    //分页查询
    @GetMapping("/list")
    public Result page(ContractPageQueryParams pageQueryParams){
        PageResult<Contract> pageResult = contractService.pageQuery(pageQueryParams);
        return Result.success().put("rows",pageResult.getRows()).put("total",pageResult.getTotal());
    }

    /**
     * 新增合同
     * @param contractDto
     * @return
     */
    @PostMapping
    public Result addContract(@RequestBody ContractDto contractDto){
        contractService.addContract(contractDto);
        return Result.success();
    }

    /**
     * 商机转客户新增合同
     * @param id
     * @param contractDto
     * @return
     */
    @PutMapping("/changeContract/{id}")
    public Result changeContract(@PathVariable Long id,@RequestBody ContractDto contractDto){
        contractService.changeContract(id,contractDto);
        return Result.success();
    }
}
